Indexed Array/Numeric Array
A Numeric Array store each element with a numeric values.
PHP Index is represent by number which start with zero.We Can Store(number,string) in the php array
All PHP array elements are asign to an Index Number.
There are two ways to create indexed arrays:
The index can be assigned automatically (index always starts at 0), like this:
$cars = array("Volvo", "BMW", "Toyota");
or the index can be assigned manually:
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";
The following example creates an indexed array named $cars, assigns three elements to it, and then prints a text containing the array values:
Example
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
Run Example>>
Loop Through an Indexed Array
To loop through and print all the values of an indexed array, you could use a for loop, like this:
Example
<?php
$cars = array("Volvo", "BMW", "Toyota");
$arrlength = count($cars);
for($x = 0; $x < $arrlength; $x++) {
echo $cars[$x];
echo "<br>";
}
?>
Run Example>>
To loop through and print all the values of an indexed array, you can also use a foreach loop, like this:
Example
<?php
$cars = array("Volvo", "BMW", "Toyota");
foreach($car as $value) {
echo $value;
echo "<br>";
}
?>
Run Example>>
Over Write Value
Example
<?php
$carname[0]="Zen";
$carname[1]="Alto";
$carname[2]="BMW";
$carname[3]="Maruti";
$carname[2]=" Honda City";//overwrite BMw with Honda City
echo $carname[0]."<br/>";
echo $carname[1]."<br/>";
echo $carname[2]."<br/>";
echo $carname[3]."<br/>";
?>
Run Example>>